package yf.mall.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.dao.MallActDao;
import yf.mall.dao.MallOrderDao;
import yf.mall.entity.MallActEntity;
import yf.mall.service.SumService;
import yf.mall.vo.OrderDownVo;
import yf.mall.vo.sum.SendGoodsVo;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author xiaolongtongxie
 * @version 1.0
 * @date 2020/5/25 15:09
 */
@Service
@Slf4j
public class SumServiceImpl implements SumService {
    @Autowired
    private MallActDao mallActDao;
    @Override
    public Integer sumUserBuyNumber(Long actId, Long userId) {
        MallActEntity actEntity = mallActDao.get(actId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return mallActDao.sumUserBuyNumber(actEntity.getStart(), actEntity.getEnd(), actEntity.getGoodsId(), userId, sdf.format(new Date()));
    }

    @Override
    public List<Object[]> sumTopUserBuy(Long actId,Integer limit) {
        MallActEntity actEntity = mallActDao.get(actId);
        return mallActDao.sumTopUserBuy(actEntity.getStart(),actEntity.getEnd(),actEntity.getGoodsId(),limit);
    }

    @SneakyThrows
    @Override
    public void sumSendGoods(String name, String date, HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
        List<SendGoodsVo> datas = mallOrderDao.sumSendGoods(name,date);
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "订单").head(SendGoodsVo.class).build();
        excelWriter.write(datas, writeSheet1);
        excelWriter.finish();
    }

    @Autowired
    private MallOrderDao mallOrderDao;
}
